import flask
from flask import Blueprint
import db

page_myinfo = Blueprint('page_myinfo', __name__)


@page_myinfo.route('/myinfo')
def myInfo():
    """
    page to show and edit current user info
    :return: null
    """
    cur_user = flask.request.cookies.get('userid')
    if cur_user is None:
        return flask.redirect('/login')
    info, up, deals, fav = db.get_user_info(userid=cur_user)
    return flask.render_template('./static/templates/myinfo.html', info=info, up=up, deals=deals, fav=fav)


@page_myinfo.route('/user_<uid>')
def user_info(uid):
    info, up, deals, fav = db.get_user_info(userid=uid)
    return flask.render_template('./static/templates/userinfo.html', info=info, up=up)


@page_myinfo.route('/modify_deal', methods=['GET', 'POST'])
def modify_deal():
    """
    modify a deal, able to change status and remove
    :return: null
    """
    req = flask.request.form
    print(req)
    db.modify_deal(req['gid'], req['buyer'], req['seller'], req['do'], req['text'])
    return 'good'


@page_myinfo.route('/remove_goods', methods=['POST'])
def rm_goods():
    return db.remove_goods(flask.request.form.get('gid'))


@page_myinfo.route('/remove_favorites', methods=['POST'])
def rm_favorites():
    info = flask.request.form
    return db.remove_favorites(info['uid'], info['gid'])


@page_myinfo.route('/modify_info', methods=['POST'])
def modify_info():
    new_info = flask.request.form
    rt = db.modify_info(new_info)
    return rt


